package com.zzyl.serve.service.impl;

import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.serve.domain.NursingProjectPlan;
import com.zzyl.serve.dto.NursingPlanDto;
import com.zzyl.serve.mapper.NursingProjectPlanMapper;
import com.zzyl.serve.service.INursingProjectPlanService;
import com.zzyl.serve.vo.NursingPlanVo;
import com.zzyl.serve.vo.NursingProjectPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingPlanMapper;
import com.zzyl.serve.domain.NursingPlan;
import com.zzyl.serve.service.INursingPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * 护理计划Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-04
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService
{
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private INursingProjectPlanService iNursingProjectPlanService;
    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    private static final String NURSINGPLAN_CACHE="nursingPlan:all";

    /**
     * 查询护理计划
     *
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlan selectNursingPlanById(Integer id)
    {
        return this.getById(id);
    }

    /**
     * 查询护理计划列表
     *
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan)

    {
        List<NursingPlan> list =(List<NursingPlan>) redisTemplate.opsForValue().get(NURSINGPLAN_CACHE);
        if(CollectionUtils.isNotEmpty(list)){
            return list;
        }
        list = nursingPlanMapper.selectNursingPlanList(nursingPlan);
        redisTemplate.opsForValue().set(NURSINGPLAN_CACHE,list);
        return list;
    }

    /**
     * 新增护理计划
     *
     * @param dto 护理计划
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertNursingPlan(NursingPlanDto dto)
    {
        NursingPlan bean = BeanUtil.toBean(dto, NursingPlan.class);
        this.save(bean);
        List<NursingProjectPlan> projectPlans = dto.getProjectPlans();
        projectPlans.forEach(p->{
            p.setPlanId(Long.valueOf((bean.getId())));
        });
        boolean b = iNursingProjectPlanService.saveBatch(projectPlans);
        deleteCache();
        return b?1:0;

    }

    /**
     * 修改护理计划
     *
     * @param dto 护理计划
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateNursingPlan(NursingPlanDto dto)
    {boolean b=true;
        NursingPlan nursingPlan = BeanUtil.toBean(dto, NursingPlan.class);
        this.updateById(nursingPlan);

        if (!CollectionUtils.isEmpty(dto.getProjectPlans())) {
            iNursingProjectPlanService.remove(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId,dto.getId()));
            List<NursingProjectPlan> projectPlans = dto.getProjectPlans();
            projectPlans.forEach(p->p.setPlanId(dto.getId()));

            b = iNursingProjectPlanService.saveBatch(dto.getProjectPlans());
        }
        deleteCache();

        return b?1:0;
    }

    /**
     * 批量删除护理计划
     *
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanByIds(Integer[] ids)
    {
        boolean b = this.removeByIds(Arrays.asList(ids));
        this.deleteCache();
        return b?1:0;
    }

    /**
     * 删除护理计划信息
     *
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteNursingPlanById(Integer id)
    {this.removeById(id);
        boolean remove = this.iNursingProjectPlanService.remove(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, id));
        deleteCache();
        return remove?1:0;
    }

    /**
     * id查询详情
     *
     * @param id
     * @return
     */
    @Override
    public NursingPlanVo getInfo(Integer id) {
        NursingPlan byId = this.getById(id);
        NursingPlanVo bean = BeanUtil.toBean(byId, NursingPlanVo.class);
        List<NursingProjectPlan> l= nursingProjectPlanMapper.getByPlanId(id);
        List<NursingProjectPlanVo> list = l.stream().map(b -> {
            NursingProjectPlanVo bean1 = BeanUtil.toBean(b, NursingProjectPlanVo.class);
            bean1.setProjectId(b.getProjectId().toString());
            bean1.setExecuteCycle(b.getExecuteCycle().toString());
            return bean1;
        }).collect(Collectors.toList());
        bean.setProjectPlans(list);

        return bean;
    }
    private void deleteCache(){
        redisTemplate.delete(NURSINGPLAN_CACHE);
    }
}
